const regeneratorRuntime = require('../../../utils/runtime.js')
const $ = require('../../../utils/util.js')
import { config } from '../../../utils/config.js'
var api = require('../../../utils/api.js')
import BluetoothLock_lxd_a from '../../../utils/lock_lxd_a.js';
const app = getApp()
var Store = app.Store

Page({
  data: {
    _height: 0,
    lock_code: '', // 扫出的码
    devices:[],
    lock:{}
  },
  search() { // 用户是否有权限操作
    console.log('-------------------',api.lockManage.lockSearch)
    return new Promise((resolve, reject) => {
      api.$get(api.lockManage.lockSearch, { 
        lockId: this.data.lock_code,
      }).then((res) => {
        // console.log(res,'1111111111111111111')
        if(res.code==200){
          resolve(res)
        }else if(res.msg=='锁具未入库'){
          setTimeout(()=>{
            $.linkurl(`/pages/lockMange/lockStation/lockStation?type=bind&name=lockScan`,'redirect')
          },1000)
        }else if(res.msg=='用户没有权限操作'){
          setTimeout(()=>{
            $.linkurl(`/pages/lockMange/lockStation/lockStation?type=apply`,'redirect') 
          },1000)
        }
      })
    })
  }, 
  onLoad:async function (options){
    wx.scanCode({
      success: async (e) => {
        this.setData({
          lock_code: e.result.replace(/:/g, '')
        })
        Store.dispatch({
          type: 'SET_LOCK_CODE',
          lock_code: this.data.lock_code
        })
        var res = await this.search()
        // console.log(res)
        if(res.code==200){
          api.$get(api.lockManage.lockKeys, {
            lockId: this.data.lock_code
          }).then(resInfo => {
            const aesKey=resInfo.data.keyHex
            const pwdBytes=resInfo.data.passwordHex
            $.linkurl(`/pages/lockMange/lockHome/lockHome?aesKey=${aesKey}&pwdBytes=${pwdBytes}`,'redirect')
          })
        }
        
      }
    }) 
  },

  onReady: function (e) {
    // this.mapCtx = wx.createMapContext('map')
  },
 
  onShow: function () {
    
  },
 




   scanCodeHandle(){ // 扫一扫
    // api.$get(api.main.lockInfo, {
    //   lock_code: '0606180312F3'
    // }).then(resInfo => {
    //   console.log("DDDDDDDDDDDDD"+resInfo)
      
    // })
    this.clearStorage()
    // wx.stopBluetoothDevicesDiscovery({
    //   success: () => {
    //     console.log('停止搜索成功');
    //   },
    //   fail: (err) => {
    //     console.log('停止搜索失败', err);
    //   }
    // });
    
  },



})
